/**
 * 
 */
package com.cssnb.nbzj.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.http.util.EncodingUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.alibaba.fastjson.JSON;
import com.cssnb.commons.utils.CharsetUtils;
import com.cssnb.nbzj.dao.CommonMapper;
import com.cssnb.wwxt.xxtx.service.IMsgService;

/**
 * @author xuning email:xuningnb@163.com
 * @version creation time:2012-5-11 类说明： 处理首页的请求
 */
@Controller
public class HomeController {

	private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
	@Autowired
	private CommonMapper commonMapper;

	@RequestMapping(method = RequestMethod.GET, value = { "/", "/index" })
	public String home(HttpSession sesison, HttpServletRequest request){
		logger.info("Welcome home! ");
		Subject currentUser = SecurityUtils.getSubject();
		String username = currentUser.getPrincipal().toString();
		logger.info("" + currentUser.isAuthenticated());
		logger.info("username:" + username);
		List subSysList = commonMapper.getSubSysList();
		subSysList = CharsetUtils.getEncodingList(subSysList, "ISO-8859-1", "GBK");
		request.setAttribute("subSysList", subSysList);
		request.setAttribute("subSysListJson", JSON.toJSONString(subSysList));
		Subject subject = SecurityUtils.getSubject();
		request.setAttribute("username", subject.getPrincipals());
		ApplicationContext ctx = new ClassPathXmlApplicationContext("rmi_client_context.xml");
		IMsgService msgService = (IMsgService) ctx.getBean("msgService");
		List xxList = msgService.getMessageList("bfs_receiver", 0);
		if(xxList.size() > 0){
			request.setAttribute("xxList", xxList);
			request.setAttribute("xxListSize", xxList.size());
		}
		return "demo/rapid/1";
	}
	@RequestMapping(method = RequestMethod.GET, value = "/main")
	public String main(@RequestParam(value = "right_id", required = false) String right_id, Model model){
		logger.info("Welcome main! ");
		// model.addAttribute("tblrights", commonManager.getDataList("tbl_right"));
		model.addAttribute("rightId", right_id);
		logger.info("param:{}", right_id);
		logger.info("method main finish");
		return "common/mainframe";
	}
	@RequestMapping(method = RequestMethod.GET, value = "/default")
	public String defaultPage(@RequestParam(value = "right_id", required = false) String right_id, Model model){
		logger.info("Welcome main! ");
		// model.addAttribute("tblrights", commonManager.getDataList("tbl_right"));
		model.addAttribute("rightId", right_id);
		logger.info("param:{}", right_id);
		logger.info("method main finish");
		return "default";
	}
	/**
	 * Hard coded test against the default user that comes with the Schema.
	 */
	@RequestMapping(value = "/smoketest", method = RequestMethod.GET)
	public String smoketest(Model model){
		// String salted = this.identityService.getApplicationSalt();
		// logger.info("SALT = " + salted);
		// String userid = "TestUser";
		// String passphrase = "TestUserPassword";
		// UsernamePasswordToken token = new UsernamePasswordToken(userid,
		// passphrase);
		// Subject currentUser = SecurityUtils.getSubject();
		//
		// try {
		// currentUser.login(token);
		// logger.info("AUTH SUCCESS");
		// } catch (AuthenticationException ae) {
		// logger.info("AUTH MSSG: " + ae.getMessage());
		// }
		//
		// Identity thisIdentity = null;
		// if (currentUser.isAuthenticated()) {
		// logger.info("PRINCIPAL: " + currentUser.getPrincipal());
		// thisIdentity =
		// identityService.getIdentity(currentUser.getPrincipal().toString());
		// salted =
		// this.identityService.getCombinedSalt(thisIdentity.getSalt());
		// logger.info("COMBINED SALT = " + salted);
		// }
		// model.addAttribute("identity", thisIdentity);
		return "smoketest/smoke";
	}
}
